package com.ymatou.im.api.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.im.api.parameter.SendMessageBean;
import com.ymatou.im.api.service.SendMessageCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.redis.ImWapper;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;



/**
 * 发送消息接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SendMessage {
	private static SendMessageBean smb;
	private static SendMessageCall smc;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发送消息接口");
	}

	@Before
	public void caseUp() {
		smb = new SendMessageBean();
		smc = new SendMessageCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(smc.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
	int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
	int customerId = Integer.valueOf(EnvSetup.getData("customerId"));
	int csId = Integer.valueOf(EnvSetup.getData("csId"));

	 @Test
	 @Category(P1.class)
	 @TestCase
	public void Tc_SendMessage_001() {
		Logger.start(true,"商家和用户发送消息,发送消息成功");
		try {
			ImDefaultWapper im = new ImDefaultWapper();
			int i = 0;
			int FromUserId = userId;
			int ToUserId = sellerId;
			String Message = "Message" + i;
			//从数据库中获取消息总数量，用于发送消息后，校验发送用户和接受用户增加消息记录数量是否正确
			Map map2=im.selectTotalCount(ToUserId, FromUserId).get(0);
			int TotalCount=(Integer)map2.get("TotalCount");
			int UnReadCount=(Integer)map2.get("UnReadCount");
			Map map3=im.selectTotalCount(FromUserId, ToUserId).get(0);
			int TotalCount2=(Integer)map3.get("TotalCount");
			int UnReadCount2=(Integer)map3.get("UnReadCount");
			//Logger.debug("TotalCount2:"+TotalCount2);
			//Logger.debug("UnReadCount2:"+UnReadCount2);
			//发送消息后消息总数量+1;发送消息后，发送消息用户增加或更新一UnReadCount值不变
			TotalCount=TotalCount+1;
			TotalCount2=TotalCount2+1;
			UnReadCount2=UnReadCount2+1;		
			//设置参数
			Logger.comment("setAppId:app.ymatou.com");
			smb.setAppId("app.ymatou.com");
			Logger.comment("setFromUserId:"+FromUserId);
			smb.setFromUserId(FromUserId);
			Logger.comment("setMessage:" + Message);
			smb.setMessage(Message);
			Logger.comment("setNeedAutoReply:false");
			smb.setNeedAutoReply(false);
			Logger.comment("setToUserId:" + ToUserId);
			smb.setToUserId(ToUserId);
			smc.setData(smb);
			smc.callService();
			
			Logger.verifyEquals(ECode.SUCCESS, smc.getString("Status"), "验证Code");	
			
			//从返回报文中获取MessageId
			Map m=(Map) smc.getMap().get("Result");
			String[] msgids=m.get("MessageId").toString().split("_");
			String msgid=msgids[msgids.length-1];			
			//验证ymt_immessage.ymt_message_61表中是否增加一条消息记录
			Map map=im.selectImInfo(ToUserId, FromUserId, Integer.parseInt(msgid)).get(0);
			int MsgFromId = (Integer)map.get("MsgFromId");
			int MsgToId= (Integer)map.get("MsgToId");
			String MessageContent=(String)map.get("Message");
	        Logger.verifyEquals(sellerId, MsgToId, "数据库验证ToUserId");	
		    Logger.verifyEquals(userId, MsgFromId, "数据库验证FromUserId");
		    Logger.verifyEquals(Message, MessageContent, "数据库验证Message");
			
		    //校验ymt_imsession.ymt_session_25中更新后的消息总数量和未读消息数
			Map map4=im.selectTotalCount(ToUserId, FromUserId).get(0);
		    int LastTotalCount=(Integer)map4.get("TotalCount");
			int LastUnReadCount=(Integer)map4.get("UnReadCount");
		    Logger.verifyEquals(TotalCount, LastTotalCount, "验证消息总数量TotalCount");   
		    Logger.verifyEquals(UnReadCount, LastUnReadCount, "验证消息未读总数量UnReadCount"); 
		    //校验ymt_imsession.ymt_session_36中更新后的消息总数量和未读消息数
		    Map map5=im.selectTotalCount(FromUserId, ToUserId).get(0);
		    int LastTotalCount2=(Integer)map5.get("TotalCount");
			int LastUnReadCount2=(Integer)map5.get("UnReadCount");
		    Logger.verifyEquals(TotalCount2, LastTotalCount2, "验证消息总数量TotalCount");   
		    Logger.verifyEquals(UnReadCount2, LastUnReadCount2, "验证消息未读总数量UnReadCount");
		    //校验写入redis内容正确性
		    ImWapper redis=new ImWapper();
		    int userid1=Integer.valueOf(redis.getLastMs(FromUserId));
		    Logger.verifyEquals(ToUserId, userid1, "验证redis中增加一条FromUserId的sessionlist");
		    
		    int userid2=Integer.valueOf(redis.getLastMs(ToUserId));
	        Logger.verifyEquals(FromUserId, userid2, "验证redis中增加一条ToUserId的sessionlist");
		    
	        int userid3=Integer.valueOf(redis.getLastUnMs(ToUserId));
	        Logger.verifyEquals(FromUserId, userid3, "验证存储redis中增加一条未读消息");
	        
		    
		    //String userid3=redis.getLastUnMs(ToUserId,1).toString();
		    //Logger.verifyEquals(FromUserId, userid2, "验证存储redis的消息记录中对应FromUserId正确性");
		   // Logger.debug("userid3:"+userid3);
		    
		    
	       // Logger.verifyEquals(updatetime,LastUpdateTime, "数据库验证ToUserId");	
			// TODO 返回信息 待验证
			// APPID 不正确时会报错
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
		/*
	    @Test
		@Category(P1.class)
		@TestCase
		public void Tc_SendMessage_002() {
			Logger.start(true,"发送多条消息成功");
			try {
				// TODO FromUserIds 写死 以后动态获取
				int[] FromUserIds={1064, 1092694, 1205, 566553, 541895, 677608, 4085, 2913, 614719, 3257216};
				int ToUserId=3383;
				for (int i = 0; i < FromUserIds.length; i++) {
					for (int loop = 0; loop < 10; loop++) {
						String Message = "Message0721001:" + loop;
						smb.setAppId("app.ymatou.com");
						int FromUserId = FromUserIds[i];
						smb.setFromUserId(FromUserId);
						smb.setMessage(Message);
						smb.setNeedAutoReply(false);
						smb.setToUserId(ToUserId);
						smc.setData(smb);
						smc.callService();
					//	TestDataManager.appendFileToLine("Test", FromUserId + ":" + ToUserId + ":" + Message);
						Logger.verifyEquals(ECode.SUCCESS, smc.getString("Status"), "验证Code");	
						Map m=(Map) smc.getMap().get("Result");
						Logger.verifyEquals(ECode.SUCCESS, m.get("FromUserName"), "验证FromUserName");	
					}
				}
				// TODO 返回信息 待验证
				// APPID 不正确时会报错
			} catch (Exception e) {
				Logger.fail(e);
			}
		}
	
		@Test
		@Category(P1.class)
		@TestCase
		public void Tc_SendMessage_003() {
			Logger.start(true,"商家和用户发送消息，接收消息用户不存在，抛异常");
			try {
				int i = 0;
				int FromUserId =userId ;
				int ToUserId =10000000;
				String Message = "Message" + i;
				Logger.comment("setAppId:app.ymatou.com");
				smb.setAppId("app.ymatou.com");
				Logger.comment("setFromUserId:"+FromUserId);
				smb.setFromUserId(FromUserId);
				Logger.comment("setMessage:" + Message);
				smb.setMessage(Message);
				Logger.comment("setNeedAutoReply:false");
				smb.setNeedAutoReply(false);
				Logger.comment("setToUserId:" + ToUserId);
				smb.setToUserId(ToUserId);
				smc.setData(smb);
				smc.callService();
				Logger.verifyEquals(ECode.ERROR500, smc.getJsonValue("Status"), "验证Code");	
				// TODO 返回信息 待验证
				// APPID 不正确时会报错
			} catch (Exception e) {
				Logger.fail(e);
			}
		}
		
		@Test
		@Category(P1.class)
		@TestCase
		public void Tc_SendMessage_004() {
			Logger.start(true,"商家和用户发送消息，发送消息用户不存在，抛异常");
			try {
				int i = 0;
				int FromUserId = 10640000;
				int ToUserId =sellerId;
				String Message = "Message" + i;
				Logger.comment("setAppId:app.ymatou.com");
				smb.setAppId("app.ymatou.com");
				Logger.comment("setFromUserId:"+FromUserId);
				smb.setFromUserId(FromUserId);
				Logger.comment("setMessage:" + Message);
				smb.setMessage(Message);
				Logger.comment("setNeedAutoReply:false");
				smb.setNeedAutoReply(false);
				Logger.comment("setToUserId:" + ToUserId);
				smb.setToUserId(ToUserId);
				smc.setData(smb);
				smc.callService();
				Logger.verifyEquals(ECode.ERROR500, smc.getJsonValue("Status"), "验证Code");	
				// TODO 返回信息 待验证
				// APPID 不正确时会报错
			} catch (Exception e) {
				Logger.fail(e);
			}
		}
		@Test
		@Category(P2.class)
		@TestCase
		public void Tc_SendMessage_005() {
			Logger.start(true,"商家和用户发送消息，消息内容大于2000字符，抛异常");
			try {
				int i = 0;
				int FromUserId =userId;
				int ToUserId =sellerId;
				String Message = "测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶；1送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯1狂的；拉萨开按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；；口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；奇偶口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口；礼疯狂的；拉萨开发按时；；；礼疯狂的；奇偶口测试卷哦是奇偶口；送大奇偶口；送大口；送大奇偶口；送大礼疯狂的；先口；送大礼疯狂的；礼疯狂的；拉萨开发按时；；礼疯狂的；奇偶  " + i;
				Logger.comment("setAppId:app.ymatou.com");
				smb.setAppId("app.ymatou.com");
				Logger.comment("setFromUserId:"+FromUserId);
				smb.setFromUserId(FromUserId);
				Logger.comment("setMessage:" + Message);
				smb.setMessage(Message);
				Logger.comment("setNeedAutoReply:false");
				smb.setNeedAutoReply(false);
				Logger.comment("setToUserId:" + ToUserId);
				smb.setToUserId(ToUserId);
				smc.setData(smb);
				smc.callService();
				Logger.verifyEquals(ECode.ERROR500, smc.getJsonValue("Status"), "验证Code");	
				// TODO 返回信息 待验证
				// APPID 不正确时会报错
			} catch (Exception e) {
				Logger.fail(e);
			}
		}
		@Test
		@Category(P2.class)
		@TestCase
		public void Tc_SendMessage_006() {
			Logger.start(true,"商家和买家发送消息，消息内容为null，抛异常");
			try {
				int FromUserId =userId;
				int ToUserId =sellerId;
				String Message = null;
				Logger.comment("setAppId:app.ymatou.com");
				smb.setAppId("app.ymatou.com");
				Logger.comment("setFromUserId:"+FromUserId);
				smb.setFromUserId(FromUserId);
				Logger.comment("setMessage:" + Message);
				smb.setMessage(Message);
				Logger.comment("setNeedAutoReply:false");
				smb.setNeedAutoReply(false);
				Logger.comment("setToUserId:" + ToUserId);
				smb.setToUserId(ToUserId);
				smc.setData(smb);
				smc.callService();
				Logger.verifyEquals(ECode.ERROR500, smc.getJsonValue("Status"), "验证Code");	
				// TODO 返回信息 待验证
				// APPID 不正确时会报错
			} catch (Exception e) {
				Logger.fail(e);
			}
		}
		
		@Test
		@Category(P2.class)
		@TestCase
		public void Tc_SendMessage_007() {
			Logger.start(true,"商家和买家发送消息，消息内容为空，抛异常");
			try {
				int FromUserId =userId;
				int ToUserId =sellerId;
				String Message ="";
				Logger.comment("setAppId:app.ymatou.com");
				smb.setAppId("app.ymatou.com");
				Logger.comment("setFromUserId:"+FromUserId);
				smb.setFromUserId(FromUserId);
				Logger.comment("setMessage:" + Message);
				smb.setMessage(Message);
				Logger.comment("setNeedAutoReply:false");
				smb.setNeedAutoReply(false);
				Logger.comment("setToUserId:" + ToUserId);
				smb.setToUserId(ToUserId);
				smc.setData(smb);
				smc.callService();
				Logger.verifyEquals(ECode.ERROR500, smc.getJsonValue("Status"), "验证Code");	
				// TODO 返回信息 待验证
				// APPID 不正确时会报错
			} catch (Exception e) {
				Logger.fail(e);
			}
		}
		
		 @Test
		 @Category(P1.class)
		 @TestCase
		public void Tc_SendMessage_008() {
			Logger.start(true,"用户给客服发送消息,AutoSetRead为false,发送消息成功");
			try {
				int i = 0;
				int FromUserId =userId;
				int ToUserId = customerId;
				//客服小号
				int CSId=csId;
				String CSName="买家客服005";
				String Message = "Message" + i;
				Logger.comment("setAppId:app.ymatou.com");
				smb.setAppId("app.ymatou.com");
				Logger.comment("setFromUserId:"+FromUserId);
				smb.setFromUserId(FromUserId);
				Logger.comment("setMessage:" + Message);
				smb.setMessage(Message);
				Logger.comment("setNeedAutoReply:false");
				smb.setNeedAutoReply(false);
				Logger.comment("setToUserId:" + ToUserId);
				smb.setToUserId(ToUserId);
				Logger.comment("setsetCSId:" +CSId );
				smb.setCSId(CSId);
				Logger.comment("setCSName:" +CSName );
				smb.setCSName(CSName);
				String SessionNumber="20160205104503_1130_3383";
				Logger.comment("setSessionNumber:" +SessionNumber );
				smb.setSessionNumber(SessionNumber);
				Logger.comment("setAutoSetRead:false ");
				smb.setAutoSetRead(false);
				smc.setData(smb);
				smc.callService();
				Logger.verifyEquals(ECode.SUCCESS, smc.getJsonValue("Status"), "验证Code");	
				// TODO 返回信息 待验证
				// APPID 不正确时会报错
			} catch (Exception e) {
				Logger.fail(e);
			}
		}
		
		 @Test
		 @Category(P1.class)
		 @TestCase
		 public void Tc_SendMessage_009() {
				Logger.start(true,"客服给用户发送消息,AutoSetRead为true,发送消息成功");
				try {
					int i = 0;
					int FromUserId =userId;
					int ToUserId = customerId;
					//客服小号
					int CSId=csId;
					String CSName="买家客服005";
					String Message = "Message" + i;
					Logger.comment("setAppId:app.ymatou.com");
					smb.setAppId("app.ymatou.com");
					Logger.comment("setFromUserId:"+FromUserId);
					smb.setFromUserId(FromUserId);
					Logger.comment("setMessage:" + Message);
					smb.setMessage(Message);
					Logger.comment("setNeedAutoReply:false");
					smb.setNeedAutoReply(false);
					Logger.comment("setToUserId:" + ToUserId);
					smb.setToUserId(ToUserId);
					Logger.comment("setsetCSId:" +CSId );
					smb.setCSId(CSId);
					Logger.comment("setCSName:" +CSName );
					smb.setCSName(CSName);
					String SessionNumber="20160205104503_1130_3383";
					Logger.comment("setSessionNumber:" +SessionNumber );
					smb.setSessionNumber(SessionNumber);
					Logger.comment("setAutoSetRead:true");
					smb.setAutoSetRead(true);
					smc.setData(smb);
					smc.callService();
					Logger.verifyEquals(ECode.SUCCESS, smc.getJsonValue("Status"), "验证Code");	
					// TODO 返回信息 待验证
					// APPID 不正确时会报错
				} catch (Exception e) {
					Logger.fail(e);
				}
			}
			
		 @Test
		 @Category(P1.class)
		 @TestCase
		 public void Tc_SendMessage_010() {
				Logger.start(true,"客服给用户发送消息,CSId不存在,发送消息失败");
				try {
					int i = 0;
					int FromUserId =userId;
					int ToUserId = customerId;
					//客服小号
					int CSId=9843922;					
					String CSName="买家客服005";
					String Message = "Message" + i;
					Logger.comment("setAppId:app.ymatou.com");
					smb.setAppId("app.ymatou.com");
					Logger.comment("setFromUserId:"+FromUserId);
					smb.setFromUserId(FromUserId);
					Logger.comment("setMessage:" + Message);
					smb.setMessage(Message);
					Logger.comment("setNeedAutoReply:false");
					smb.setNeedAutoReply(false);
					Logger.comment("setToUserId:" + ToUserId);
					smb.setToUserId(ToUserId);
					Logger.comment("setsetCSId:" +CSId );
					smb.setCSId(CSId);
					Logger.comment("setCSName:" +CSName );
					smb.setCSName(CSName);
					String SessionNumber="20160205104503_1130_3383";
					Logger.comment("setSessionNumber:" +SessionNumber );
					smb.setSessionNumber(SessionNumber);
					Logger.comment("setAutoSetRead:true");
					smb.setAutoSetRead(true);
					smc.setData(smb);
					smc.callService();
					Logger.verifyEquals(ECode.ERROR500, smc.getJsonValue("Status"), "验证Code");	
					// TODO 返回信息 待验证
					// APPID 不正确时会报错
				} catch (Exception e) {
					Logger.fail(e);
				}
			}	
		 */
}
